# Updated by Wei 2013/02/21
# program purpose: add the rank_in_the_list into the main training file

# step1:
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded_tail_500K_sorted_by_queryID_rankInInvertedListAdded"
inputFileHandler = open(inputFileName,"r")

rankDict = {}

for index,line in enumerate( inputFileHandler.readlines() ):
    lineElements = line.strip().split(" ")
    uniqueIdentifyString = lineElements[0] + "_" + lineElements[1] + "_" + lineElements[2]
    testString = lineElements[0] + " " + lineElements[1] + " " + lineElements[2]
    if uniqueIdentifyString not in rankDict:
        rankDict[uniqueIdentifyString] = lineElements[-1]
    else:
        pass
        #print "index:",index,"uniqueIdentifyString:",testString
print "len(rankDict):",len(rankDict)
inputFileHandler.close()

#step2:
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded_tail_500K_weiHLFeaturesAdded"
inputFileHandler = open(inputFileName,"r")
infoHeadLine = inputFileHandler.readline()
newInfoHeadLine = infoHeadLine.strip() + " " + "posting_rank_in_inverted_list" + "\n"

outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded_tail_500K_weiAllPreparedFeaturesAdded"
outputFileHandler = open(outputFileName,"w")
outputFileHandler.write(newInfoHeadLine)
for line in inputFileHandler.readlines():
    lineElements = line.strip().split(" ")
    uniqueIdentifyString = lineElements[0] + "_" + lineElements[1] + "_" + lineElements[2]
    if uniqueIdentifyString in rankDict:
        outputFileHandler.write(line.strip() + " " + rankDict[uniqueIdentifyString] + "\n")
    else:
        print "system error"
        print "uniqueIdentifyString:",uniqueIdentifyString
        print "line:",line.strip()
        
        
print "Program Ends"
    
    
    
    
    
    
    
    
    
    